#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
sem_t sem;
void* consumer_p(void*)
{
    int val;
    while(1)
    {
        usleep(1);
        sem_post(&sem);
        sem_getvalue(&sem, &val);
        printf("生产, 总量:%d\n", val);
    }
    return NULL;
}

void* consumer_f(void*)
{
    int val;
    while(1)
    {
        usleep(1);
        sem_wait(&sem);
        sem_getvalue(&sem, &val);
        printf("消费, 总量:%d\n", val);
    }
    return NULL;
}
int main()
{
    pthread_t th1, th2;
    int ret = -1;
    sem_init(&sem, 0, 0);
    pthread_attr_t th_attr;
    pthread_attr_init(&th_attr);
    pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_DETACHED);
    ret = pthread_create(&th1, &th_attr, consumer_p, NULL);
    ret = pthread_create(&th2, &th_attr, consumer_f, NULL);
    pthread_attr_destroy(&th_attr);
    while(1);
    sem_destroy(&sem);
    return 0;
}
